package cn.sdormitory.smartdor.service.impl;

import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.basedata.service.BStudentService;
import cn.sdormitory.common.utils.BeanUtils;
import cn.sdormitory.smartdor.dao.StudentTrajectoryDao;
import cn.sdormitory.smartdor.entity.StudentTrajectory;
import cn.sdormitory.smartdor.entity.StudentTrajectoryExcel;
import cn.sdormitory.smartdor.entity.StudentTrajectoryPageParam;
import cn.sdormitory.smartdor.service.StudentTrajectoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class StudentTrajectoryServiceImpl extends ServiceImpl<StudentTrajectoryDao, StudentTrajectory> implements StudentTrajectoryService {

    @Resource
    private StudentTrajectoryDao studentTrajectoryDao;
    @Resource
    private BStudentService bStudentService;

    @Override
    public List<StudentTrajectory> getStuTrajectory(Long studentId) {
//        String tenantId = SysUserUtils.getTenantId();
        Page<StudentTrajectory> page = new Page<>(1, 20);
        LambdaQueryWrapper<StudentTrajectory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentTrajectory::getStudentId,studentId).orderByDesc(StudentTrajectory::getFaceTime);
        List<StudentTrajectory> records = studentTrajectoryDao.selectPage(page, wrapper).getRecords();
        if(records.isEmpty()){
            return records;
        }
        BStudent studentInfo = bStudentService.getOne(Wrappers.<BStudent>lambdaQuery()
                .select(BStudent::getStudentName, BStudent::getClassName)
                .eq(BStudent::getId, studentId)
        );
        if(studentInfo!= null){
            for (StudentTrajectory record : records) {
                record.setStudentName(studentInfo.getStudentName());
                record.setClassName(studentInfo.getClassName());
            }
        }
        return records;
    }

    @Override
    public IPage<StudentTrajectory> findPageList(StudentTrajectoryPageParam query) {
        int startNum = (query.getPageNum() - 1 ) * query.getPageSize();

        int count = studentTrajectoryDao.findPageCount(query);
        List<StudentTrajectory> list = studentTrajectoryDao.findPageList(startNum,query);
        if(query.isQueryDormitoryInfo()){
            list.forEach(StudentTrajectory::initDormitoryStatus);
        }
        IPage<StudentTrajectory> page = new Page<>();
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<StudentTrajectory> findList(StudentTrajectoryPageParam query) {
        query.setPageSize(10000);
        return studentTrajectoryDao.findPageList(0,query);
    }

    @Override
    public IPage<StudentTrajectory> teacherFindPageList(StudentTrajectoryPageParam query) {
        int startNum = (query.getPageNum() - 1 ) * query.getPageSize();
        int count = studentTrajectoryDao.teacherFindPageListCount(query);
        List<StudentTrajectory> list = studentTrajectoryDao.teacherFindPageList(startNum,query);
        IPage<StudentTrajectory> page = new Page<>();
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<StudentTrajectoryExcel> exportTrajectory(StudentTrajectoryPageParam query) {
        List<StudentTrajectory> list = findList(query);
        return new ArrayList<>();
    }
}
